<!--
 * @Date: 2022-12-08 13:46:18
 * @LastEditTime: 2023-01-07 09:34:58
 * @Description: content
-->
<template>
	<view class="page-wrap common_bg" :style="{
      backgroundImage: `url(${imgBaseUrl}${'/static/img/draw_page_bg.png'})`,
    }">
		<template v-if="!popShow">
			<!-- <u-gap height="88"></u-gap> -->

			<view class="draw-wrap" :style="{
          opacity: initOk ? 1 : 0,
        }">
				<view class="line">
					<view class="icon">
						<cimage src="/static/icon/dot.png" mode="scaleToFill" />
					</view>

					<view class="icon">
						<cimage src="/static/icon/dot.png" mode="scaleToFill" />
					</view>

					<view class="line-inner"></view>
				</view>

				<view class="draw-wrap-hd common_bg" :style="{
            backgroundImage: `url(${imgBaseUrl}${'/static/img/draw_hd_bg.png'})`,
          }">
					<view class="title" v-if="drawNum == 1">一发入魂</view>
					<view class="title" v-if="drawNum == 2">幸运二连</view>
					<view class="title" v-if="drawNum == 3">三发入魂</view>
					<view class="title" v-if="drawNum == 4">霸气四连</view>
					<view class="title" v-if="drawNum == 5">五连绝世</view>
				</view>

				<view class="draw-wrap-bd">
					<view v-for="(item, i) in drawList" :key="i" class="row" :style="{
              transform: `translateX(${item.translate}px)`,
              'transition-property': drawState == 'start' ? 'all' : 'none',
              'transition-duration': `${item.duration}s`,
            }">
						<view v-for="(a, b) in item.list" :key="b" class="row-item">
							<view class="pic" :style="{
                  backgroundImage: `url(${a.thumb})`,
                }"></view>

							<view class="bar"></view>
						</view>
					</view>
				</view>

				<view class="draw-wrap-ft common_bg" :style="{
            backgroundImage: `url(${imgBaseUrl}${'/static/img/draw_ft_bg.png'})`,
          }"></view>
			</view>

			<view class="ft-btn">
				<view @click="stop" v-if="drawState == 'start'" class="btn">跳过</view>

				<view v-else class="btn hide"></view>
			</view>
		</template>

		<scroll-view class="prize-pop-wrap" v-else scroll-y>
			<view class="prize-pop common_bg" :style="{
          backgroundImage: `url(${imgBaseUrl}${'/static/img/draw_page_bg.png'})`,
        }">
				<uni-nav-bar color="#fff" leftIcon="left" backgroundColor="transparent" :border="false"
					:statusBar="true" :fixed="true" @clickLeft="$common.back()"></uni-nav-bar>

				<view class="open-title common_bg" :style="{
            backgroundImage: `url(${imgBaseUrl}${'/static/img/gong_xi_huo_de.png'})`,
          }"></view>

				<view v-if="prizeResult.length == 1" class="prize-box">
					<view v-for="(item, i) in prizeResult" :key="i" class="prize-box-item common_bg upin" :style="{
              backgroundImage: `url(${item.mark_bg_thumb})`,
            }">
						<view class="pic">
							<cimage :src="item.thumb" mode="scaleToFill" />
						</view>

						<view class="title hang1">
							{{ item.title }}
						</view>
						<view class="title hang1">
							{{ item.mark_title }}
						</view>

						<view class="price">
							参考价:

							<text>
								¥

								<text>{{ item.price }}</text>
							</text>
						</view>

						<!-- 			<button class="dissolve_btn" @click="duihuan(item.order_list_id)" v-if="rj && !item.is_hs && optionsData.type != 'play'">溶解{{item.dissolve_price}}￥</button>
 -->
						<button class="dissolve_btn" @click="duihuan(item.order_list_id)" v-if="rj && item.is_hs"
							disabled>
							已溶解
						</button>
					</view>
				</view>

				<view v-if="prizeResult.length == 2" class="prize-box three">
					<view v-for="(item, i) in prizeResult" :key="i" class="prize-box-item common_bg upin" :style="{
              backgroundImage: `url(${item.mark_bg_thumb})`,
              'animation-delay': `${i * 0.2}s`,
            }">
						<view class="pic">
							<cimage :src="item.thumb" mode="scaleToFill" />
						</view>

						<view class="title hang1">
							{{ item.title }}
						</view>
						<view class="title hang1">
							{{ item.mark_title }}
						</view>

						<view class="price">
							参考价:

							<text>
								¥

								<text>{{ item.price }}</text>
							</text>
						</view>
						<!-- <button class="dissolve_btn" @click="duihuan(item.order_list_id)" v-if="rj && !item.is_hs && optionsData.type != 'play'">溶解{{item.dissolve_price}}￥</button> -->
						<button class="dissolve_btn" @click="duihuan(item.order_list_id)"
							v-if="(rj && item.is_hs) || is_hsa" disabled>
							已溶解
						</button>
					</view>
				</view>

				<view v-if="prizeResult.length == 3" class="prize-box three">
					<view v-for="(item, i) in prizeResult" :key="i" class="prize-box-item common_bg upin" :style="{
              backgroundImage: `url(${item.mark_bg_thumb})`,
              'animation-delay': `${i * 0.2}s`,
            }">
						<view class="pic">
							<cimage :src="item.thumb" mode="scaleToFill" />
						</view>

						<view class="title hang1">
							{{ item.title }}
						</view>
						<view class="title hang1">
							{{ item.mark_title }}
						</view>

						<view class="price">
							参考价:

							<text>
								¥

								<text>{{ item.price }}</text>
							</text>
						</view>
						<!-- <button class="dissolve_btn" @click="duihuan(item.order_list_id)" v-if="rj && !item.is_hs && optionsData.type != 'play'">溶解{{item.dissolve_price}}￥</button> -->
						<button class="dissolve_btn" @click="duihuan(item.order_list_id)"
							v-if="(rj && item.is_hs) || is_hsa" disabled>
							已溶解
						</button>
					</view>
				</view>

				<view v-if="prizeResult.length == 4" class="prize-box five">
					<view v-for="(item, i) in prizeResult" :key="i" class="prize-box-item common_bg upin" :style="{
              backgroundImage: `url(${item.mark_bg_thumb})`,
              'animation-delay': `${i * 0.2}s`,
            }">
						<view class="pic">
							<cimage :src="item.thumb" mode="scaleToFill" />
						</view>

						<view class="title hang1">
							{{ item.title }}
						</view>
						<view class="title hang1">
							{{ item.mark_title }}
						</view>

						<view class="price">
							参考价:

							<text>
								¥

								<text>{{ item.price }}</text>
							</text>
						</view>
						<!-- <button class="dissolve_btn" @click="duihuan(item.order_list_id)" v-if="rj && !item.is_hs && optionsData.type != 'play' ">溶解{{item.dissolve_price}}￥</button> -->
						<button class="dissolve_btn" @click="duihuan(item.order_list_id)" v-if="rj && item.is_hs"
							disabled>
							已溶解
						</button>
					</view>
				</view>

				<view v-if="prizeResult.length >= 5" class="prize-box five">
					<view v-for="(item, i) in prizeResult" :key="i" class="prize-box-item common_bg upin" :style="{
              backgroundImage: `url(${item.mark_bg_thumb})`,
              'animation-delay': `${i * 0.2}s`,
            }">
						<view class="pic">
							<cimage :src="item.thumb" mode="scaleToFill" />
						</view>

						<view class="title hang1">
							{{ item.title }}
						</view>
						<view class="title hang1">
							{{ item.mark_title }}
						</view>

						<view class="price">
							参考价:

							<text>
								¥

								<text>{{ item.price }}</text>
							</text>
						</view>
						<!-- <button class="dissolve_btn" @click="duihuan(item.order_list_id)" v-if="rj && !item.is_hs && optionsData.type != 'play'">溶解{{item.dissolve_price}}￥</button> -->
						<button class="dissolve_btn" @click="duihuan(item.order_list_id)" v-if="rj && item.is_hs"
							disabled>
							已溶解
						</button>
					</view>
				</view>

				<view class="ft-btn">
					<view v-if="optionsData.type != 'play'" @click="toBag" class="btn"> 立即提货 </view>
					<!-- <view v-if="rj && optionsData.type != 'play'" @click="duihuan('-1')" class="btn" disabled>
            {{is_hsa != false ? '溶解成功' : '全部溶解'}}
          </view> -->
					<view @click="again" class="btn">再来一次</view>

					<view v-if="optionsData.type == 'play'" @click="$common.back()" class="btn"> 来把真的 </view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from "vuex";

	export default {
		data() {
			return {
				is_hsa: false,
				optionsData: "",
				drawNum: 5,
				/* 每行奖品长度 */
				rowLenght: 30,
				/* 抽奖转动时间 */
				duration: 0,
				initOk: false, // 是否已完成初始化
				minLen: 15, // 抽奖列表长度
				drawState: "stop", // 抽奖状态
				drawTimer: null, // 抽奖定时器
				itemWidth: 0, // 每个奖品的宽度
				/* 奖品弹窗显示 */
				popShow: false,
				drawList: [],
				/* 中奖列表 */
				prizeResult: [],
				all: false,
				rj: false,
			};
		},

		computed: {
			...mapGetters(["ingMusic"]),
		},
		onLoad(options) {
			this.req({
				url: "/v1/rongjie",
				success: (res) => {
					if (res.code == 200) {
						this.rj = res.data;
					}
				},
			});

			this.optionsData = options;
			this.drawNum = options.drawNum;
			console.log(options.stop)
			if (!options.stop || options.stop == "false" || options.stop == false) {
				this.stop();
			}
		},

		onReady() {
			this.getData();
		},

		onUnload() {
			this.ingMusic.stop();
		},

		methods: {
			/**
			 * @description: 再来一次
			 * @return {*}
			 */
			again() {
				if (this.optionsData.type == "play") {
					this.popShow = false;
					this.initOk = false;
					this.drawList = [];
					this.getData();
				} else {
					uni.$emit("again", {});
					this.$common.back();
				}
			},

			/**
			 * @description: 去提货
			 * @return {*}
			 */
			toBag() {
				this.$common.to({
					type: 2,
					url: "/package/mine/bag",
				});
			},

			/**
			 * @description: 开奖
			 * @return {*}
			 */
			openBox() {
				return new Promise((resolve, reject) => {
					let url = "/v1/box/award";
					let data = {};

					if (this.optionsData.type == "play") {
						url = "/v1/box/order/tryplay";
						data.id = this.optionsData.id;
					} else {
						data.order_sn = this.optionsData.order_sn;
						data.box_id = this.optionsData.id;
					}

					this.req({
						url,
						data,
						Loading: false,
						success: (res) => {
							if (res.code == 200) {
								this.prizeResult = res.data.data;
								this.drawNum = res.data.data.length;
								resolve();

								// this.draw(res.data)
							}
						},
					});
				});
			},

			/**
			 * @description: 获取数据
			 * @return {*}
			 */
			getData() {
				this.req({
					url: "/v1/box/info",
					data: {
						id: this.optionsData.id,
					},
					Loading: true,
					success: async (res) => {
						if (res.code == 200) {
							await this.openBox();

							this.init({
								list: res.data.awardList
							});
						}
					},
				});
			},
			/**
			 * @description: 粉碎商品
			 * @param {*}
			 * @return {*}
			 */
			duihuan(id) {
				let arr = Array.from(Object.values(this.prizeResult), (x) => x);
				if (this.is_hsa != false) {
					this.$common.toast({
						title: "已全部溶解了",
						icon: "error",
						duration: 600,
					});
					return;
				}
				if (id == "-1") {
					let ids = [];
					this.all = true;
					arr.forEach((val, index) => {
						ids.push(val.order_list_id);
					});
					id = ids;
				}
				this.req({
					url: "/v1/user/ship/box/dissolve",
					data: {
						id: id,
					},
					success: (res) => {
						if (res.code == 200) {
							this.$common.toast({
								title: res.msg,
								icon: "success",
								duration: 600,
								success: () => {
									if (this.all) {
										this.is_hsa = true;
									}
									arr.forEach((val, index) => {
										if (val.order_list_id == id || this.all) {
											val.is_hs = true;
										}
									});
									this.prizeResult = arr;
								},
							});
						}
					},
				});
			},
			/**
			 * @description: 停止滚动
			 * @param {*} res
			 * @return {*}
			 */
			stop() {
				this.drawState = "stop";
				this.drawTimer && clearTimeout(this.drawTimer);
				this.drawTimer = null;
				// this.duration = 0

				/* 停止滚动 */
				this.drawList.map((item) => {
					item.duration = 0;
				});
				this.ingMusic.stop();

				this.popShow = true;
			},

			/**
			 * @description: 抽奖
			 * @param {*} res 中奖位置
			 * @return {*}
			 */
			draw(res) {
				console.log(res, 111);
				if (this.drawState == "start") {
					console.warn("正在抽奖中");
					return;
				}
				console.log("开始了", res);
				this.drawState = "start";
				this.prizeResult = res;
				let arr = JSON.parse(JSON.stringify(this.drawList));
				arr.map((item, i) => {
					/* 设置滚动位置 */
					item.translate = (item.list.length - 3) * this.itemWidth * -1;

					/* 将中奖奖品放入抽奖列表中 */
					item.list[item.list.length - 3] = res[i];
				});

				this.drawList = arr;
				console.log("list", this.drawList);

				console.log("即将停止");
				/* 动画结束后停止 */

				this.ingMusic.startTime = 0.9;
				this.ingMusic.stop();
				this.ingMusic.play();
				this.drawTimer = setTimeout(() => {
					this.stop();
				}, this.duration);
			},

			/**
			 * @description: 初始化
			 * @param {*} list  滚动列表
			 * @param {*} duration  抽奖转动时间
			 * @return {*}
			 */
			init({
				list,
				duration = 9000
			}) {
				console.log(list, "list");
				let prizeList = JSON.parse(JSON.stringify(list));

				/* 如果奖品列表长度不满每行长度,补满 */
				if (prizeList.length < this.rowLenght) {
					for (let i = 0; prizeList.length < this.rowLenght; i++) {
						if (i == list.length) {
							i = 0;
						}

						prizeList.push(list[i]);
					}
					/* 奖品列表长度大于每行长度,截取对应长度 */
				} else {
					prizeList = prizeList.slice(0, this.rowLenght);
				}
				console.log(prizeList, "prizeList");
				let arr2 = [];
				/* 根据抽奖次数生成二维数组 */
				for (let i = 0; i < this.drawNum; i++) {
					/* 对数组进行打乱,并插入 */
					let list = JSON.parse(JSON.stringify(prizeList)).sort(() => {
						return Math.random() < 0.5 ? 1 : -1;
					});

					/* 随机向数组中插入一个元素 */
					if (i % 2 == 0) {
						list.push(list[Math.floor(Math.random() * list.length)]);
					}

					arr2.push({
						list
					});
				}
				console.log(arr2, "arr2");
				// this.drawList = arr2
				// console.log(arr2)

				this.$nextTick(() => {
					this.duration = duration;

					const query = uni.createSelectorQuery().in(this);
					query
						.select(".draw-wrap-bd")
						.boundingClientRect((data) => {
							this.itemWidth = data.width;

							arr2.map((item) => {
								item.translate = data.width * -2;
								item.duration = duration / 1000;
							});
							this.drawList = arr2;

							console.log(this.drawList);
						})
						.exec((res) => {
							// console.log('res`````', res)
							this.initOk = true;

							setTimeout(() => {
								// this.openBox()
								this.draw(this.prizeResult);
							}, 100);
						});
				});
			},
		},
	};
</script>

<style lang="scss">
	@keyframes zoom-in {
		0% {
			transform: scale(0, 0);
			opacity: 0;
		}

		100% {
			transform: scale(1, 1);
			opacity: 1;
		}
	}

	.zoom-in {
		animation: zoom-in 0.5s;
	}

	@keyframes upin {
		0% {
			transform: translateY(-100vh);
			opacity: 0;
		}

		100% {
			transform: translateY(0);
			opacity: 1;
		}
	}

	.upin {
		animation-name: upin;
		animation-duration: 0.5s;
		animation-fill-mode: forwards;
	}

	.page-wrap {
		display: flex;
		flex-flow: column nowrap;
		justify-content: center;
		min-height: 100vh;
		overflow-x: hidden;

		/deep/.uni-navbar {
			position: fixed;
		}

		/deep/.uni-navbar__content {
			top: 0;
		}

		.draw-wrap {
			position: relative;

			.line {
				position: absolute;
				z-index: 10;
				left: 50%;
				top: 140rpx;
				width: 50rpx;
				height: calc(100% - 168rpx);
				transform: translateX(-50%);
				box-sizing: border-box;
				padding: 24rpx 0;
				display: flex;
				justify-content: center;

				.icon {
					position: absolute;
					z-index: 1;
					width: 50rpx;
					height: 50rpx;
					left: 0;
					top: 0;
				}

				.icon+.icon {
					top: auto;
					bottom: 0;
				}

				&-inner {
					width: 4rpx;
					height: 100%;
					background: #ffffff;
				}
			}

			&-hd {
				width: 100%;
				height: 230rpx;
				text-align: center;
				box-sizing: border-box;
				padding-top: 90rpx;

				.title {
					font-size: 40rpx;
					font-weight: bold;
					transform: skew(-10deg);
					color: #ffffff;
					text-shadow: 1rpx 4rpx 20rpx rgba(87, 103, 229, 1);
				}
			}

			&-bd {
				width: 150rpx;
				margin: 0 auto;
				transform: translate3d(0, 0, 0);
				perspective: 1000;

				.row {
					width: 100%;
					height: 150rpx;
					display: flex;

					&-item {
						width: 100%;
						height: 100%;
						position: relative;
						display: flex;
						justify-content: center;
						align-items: center;

						&:nth-child(4n-3) {
							.bar {
								background: #4534fe;
							}
						}

						&:nth-child(4n-2) {
							.bar {
								background: #fc474e;
							}
						}

						&:nth-child(4n-1) {
							.bar {
								background: #81beff;
							}
						}

						&:nth-child(4n) {
							.bar {
								background: #f8c84c;
							}
						}

						.pic {
							width: 150rpx;
							height: 150rpx;
							box-sizing: border-box;
							padding: 34rpx;
							position: relative;
							z-index: 2;
							background-size: 82rpx 82rpx;
							background-repeat: no-repeat;
							background-position: center center;
						}

						.bar {
							position: absolute;
							bottom: 0;
							left: 0;
							width: 100%;
							height: 30rpx;
							transform: skew(-10deg);
							box-shadow: inset 0rpx 5rpx 1rpx 0rpx rgba(255, 255, 255, 0.23),
								inset 0rpx 0rpx 5rpx 0rpx rgba(0, 0, 0, 0.5);
						}
					}
				}
			}

			&-ft {
				width: 100%;
				height: 168rpx;
				margin-top: -40rpx;
			}
		}

		.ft-btn {
			display: flex;
			justify-content: space-around;
			padding: 20rpx 0;
			flex-flow: wrap;

			.btn {
				width: 303rpx;
				height: 109rpx;
				background: #000000;
				border-radius: 10rpx;
				line-height: 109rpx;
				text-align: center;
				box-shadow: inset 0 0 40rpx 0 #61708f;

				font-size: 40rpx;
				font-weight: bold;
				font-style: italic;
				color: #ffffff;

				&.hide {
					opacity: 0;
				}
			}

			.btn+.btn {
				color: #ff6666;
			}

			.btn+.btn+.btn {
				margin-top: 20rpx;
				color: #55aaff;
			}
		}

		.prize-pop-wrap {
			width: 100vw;
			height: 100vh;
			position: fixed;
			left: 0;
			top: 0;

			.prize-pop {
				width: 100vw;
				min-height: 100vh;
				z-index: 10;
				display: flex;
				flex-flow: column nowrap;
				justify-content: center;

				.open-title {
					width: 100%;
					height: 116rpx;
				}

				.prize-box {
					display: flex;
					flex-flow: row wrap;
					justify-content: center;
					align-items: center;
					padding: 40rpx 0;

					&-item {
						width: 428rpx;
						height: 544rpx;
						box-sizing: border-box;
						padding-top: 140rpx;
						display: flex;
						flex-flow: column nowrap;
						align-items: center;
						transform: translateY(-100vh);

						.pic {
							width: 200rpx;
							height: 200rpx;
						}

						.title {
							font-size: 26rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #ffffff;
							width: 100%;
							box-sizing: border-box;
							padding: 30rpx 30rpx;
							text-align: center;
						}

						.price {
							width: 100%;
							box-sizing: border-box;
							padding: 0 30rpx;

							font-size: 26rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #ffffff;
							text-align: center;

							text {
								font-size: 24rpx;
								font-weight: bold;

								text {
									font-size: 32rpx;
								}
							}
						}

						.dissolve_btn {
							width: 240rpx;
							height: 68rpx;
							border: none;
							margin-top: 20rpx;
							line-height: 68rpx;
							font-size: 30rpx;
						}
					}
				}

				.three {
					.prize-box-item {
						width: 314rpx;
						height: 400rpx;
						padding-top: 94rpx;

						.pic {
							width: 170rpx;
							height: 170rpx;
						}

						.title {
							font-size: 22rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #ffffff;
							padding: 14rpx 30rpx;
						}

						.price {
							font-size: 20rpx;
							padding: 0rpx 30rpx;

							text {
								font-size: 20rpx;

								text {
									font-size: 24rpx;
								}
							}
						}

						.dissolve_btn {
							width: 180rpx;
							height: 56rpx;
							border: none;
							margin-top: 15rpx;
							line-height: 56rpx;
							font-size: 18rpx;
						}
					}
				}

				.five {
					padding: 40rpx 0rpx;

					.prize-box-item {
						width: 230rpx;
						height: 300rpx;
						padding-top: 70rpx;

						// &:nth-child(3) {
						//   margin: 0 100rpx;
						// }

						.pic {
							width: 120rpx;
							height: 120rpx;
						}

						.title {
							font-size: 18rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #ffffff;
							padding: 10rpx 20rpx;
						}

						.price {
							font-size: 16rpx;
							padding: 0rpx 20rpx;

							text {
								font-size: 16rpx;

								text {
									font-size: 18rpx;
								}
							}
						}

						.dissolve_btn {
							width: 180rpx;
							height: 52rpx;
							border: none;
							margin-top: 12rpx;
							line-height: 40rpx;
							font-size: 17rpx;
						}
					}
				}
			}
		}
	}
</style>
